<template>
	<view>
		<button @click="cha" v-if="false">查</button>
		<view class="contents">
			<template v-if="info.state==1">
				<view class="image">
					<image 
						src="https://aihelp.oss-cn-hangzhou.aliyuncs.com/public/base/2023/11/14/55168fd3ddb94f59946049e3cf48d5d2.png"
						mode="widthFix"></image>
				</view>
				<view class="text font13 text-center color666 u-font-26">
					<view style="width: 600rpx;">
						已经生成个性化的报告，可下载查看啦，下载需要几秒钟。文件保存在Download/WeiXin
						</text>
					</view>
				</view>
			</template>
			<template v-if="info.state === 0">
				<view class="image">
					<image 
						src="https://aihelp.oss-cn-hangzhou.aliyuncs.com/public/base/2023/11/14/e4bb75e5b48544a18b27102b37d80eb2.png"
						mode="widthFix"></image>
				</view>
				<view class="text font13 text-center color666 u-font-26">
					<view style="width: 600rpx;">
						正在生成报告，数据很多，请耐心等待......</text>
					</view>
				</view>
			</template>
			<template v-if="info.state === 2">
				<view class="image">
				<view class="image">
					<image show-menu-by-longpress style="height: 200rpx;" :src="src1" mode="widthFix"></image>
				</view>
				</view>
				<view class="text font13 text-center color666 u-font-26">
					<view style="width: 600rpx;">
						报告生成失败，请联系客服进行处理</text>
					</view>
				</view>
			</template>
			<view>
				<view class="box">
					<view class="font12 u-m-l-30 u-m-r-20">
						{{info.name || ''}}
					</view>
					<view class="font12 u-m-r-30 shrink-0" style="color: #F7DD5E;" v-if="info.state==0">
						生成中...
					</view>
					<view class="font12 btn shrink-0" v-if="info.state==1" @click="download(info)">
						可下载
					</view>
					<view class="font12 btn shrink-0" v-if="info.state == 2">
						下载失败
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		debounce
	} from "@/utils/common.js"
	import {
		getPDFList,
		getchoosePDF,
		serveUser
	} from '@/api/toolbox.js'
	export default {
		data() {
			return {
				id: '',
				school: {},
				isShow: 0,
				timer: null,
				total: '',
				pdfList: '',
				pdf: {},
				info: {},
				src1:""
			};
		},
		onShow() {
			let that = this
			this.timer = setInterval(function() {
				that.getPDFData()
			}, 10000)
		},
		onLoad(option) {
			this.id = option.id
			this.school = uni.getStorageSync('school')
			this.getPDFData()
			serveUser().then((res) => {
				this.src1 = res.data
			})
			
		},
		onHide() {
			if (this.timer) {
				clearInterval(this.timer);
				this.timer = null;
			}
		},
		onUnload() {
			if (this.timer) {
				clearInterval(this.timer);
				this.timer = null;
			}
		},
		methods: {
			cha() {

				console.log(this.pdfList)
			},
			async getPDF() {
				const data = {}
				data.schoolCode = this.school.schoolCode
				data.year = this.school.year
				data.collegeCode = this.school.collegeCode
				data.majorCode = this.school.majorCode
				data.userId = uni.getStorageSync('userId')
				data.orderId = 0
				data.reportId = 0
				data.price = 0
				data.page = 1
				data.pageSize = 10
				data.type = 1
				const res = await getPDFList(data)
				this.isShow = 1
			},
			async getPDFData() {
				const res = await getchoosePDF({
					page: 1,
					pageSize: 10,
					type: 1,
					reportId: this.id
				})
				this.info = res.data.rows[0];
			},
			getDownload() {
				debounce(() => {
					this.getPDF()
				}, 1000)
			},
			// #ifdef MP-WEIXIN
			download(item) {
				let that = this
				uni.showLoading({
					title: '下载中...',
					mask: true
				});
				uni.downloadFile({
					url: item.url, //下载地址，后端接口获取的链接
					success: (data) => {
						console.log(data.tempFilePath)
						console.log(JSON.stringify(data))
						if (data.statusCode === 200) {
							let a = uni.getFileSystemManager()
							a.saveFile({ //文件保存到本地
								tempFilePath: data.tempFilePath, //临时路径
								success: function(res) {
									console.log("下载成功" + res.savedFilePath)
									console.log(JSON.stringify(res))
									uni.showToast({
										icon: 'none',
										mask: true,
										title: '文件已保存!',
										duration: 3000,
									});
									uni.openDocument({
										//fileType: 'docx',
										showMenu: true, //关键点,可以转发到微信
										filePath: res.savedFilePath,
										success: function(res) {
											console.log('打开文档成功');
										}
									});
								}
							});
						}
					},
					fail: (err) => {
						console.log(err);
						uni.showToast({
							icon: 'none',
							mask: true,
							title: '失败请重新下载',
						});
					},
					complete: (res) => {
						uni.hideLoading();
					}
				});
			}
			// #endif
			// #ifdef APP-PLUS
			download(item) {
				uni.downloadFile({
					url: item.url,
					success: function(res) {
						var filePath = res.tempFilePath;
						uni.openDocument({
							filePath: filePath,
							showMenu: true,
							success: function(res) {
								console.log('打开文档成功');
							}
						});
					}
				});
			}
			// #endif
			// #ifdef H5
			download(item) {
				window.open(item.url)
			}
			// #endif
		}
	}
</script>


<style>
	page {
		background-color: #F6F8F6;
	}
</style>

<style lang="scss">
	.contents {
		.image {
			display: flex;
			justify-content: center;
			width: 400rpx;
			align-items: center;
			margin: 60rpx auto 40rpx;
		}

		.text {
			display: flex;
			justify-content: center;
		}

		.box {
			margin: 30rpx;
			width: 92vw;
			height: 100rpx;
			background: #fff;
			border-radius: 10rpx;
			display: flex;
			justify-content: space-between;
			align-items: center;

			.btn {
				width: 128rpx;
				height: 54rpx;
				background-color: #F7DD5E;
				border-radius: 54rpx;
				text-align: center;
				line-height: 54rpx;
				margin-right: 30rpx;
			}

			.btn2 {
				width: 158rpx;
				height: 54rpx;
				background-color: #F7DD5E;
				border-radius: 54rpx;
				text-align: center;
				line-height: 54rpx;
				margin-right: 30rpx;
			}
		}
	}
</style>